package com.handpay.zztong.hp.main.activity;

import android.os.Bundle;
import android.widget.GridView;
import com.handpay.framework.notice.ApiNoticeFactory;
import com.handpay.framework.notice.ApiNoticeListener;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.db.ormlite.DataBaseComponentModel;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.main.model.FunctionInfo;
import com.handpay.zztong.hp.main.model.component.ComponentMappingItemBean;
import com.handpay.zztong.hp.main.widget.icon.IconBaseAdapter;
import com.handpay.zztong.hp.main.widget.icon.IconBaseOnItemClickListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyang on 2017/1/9.
 */


public class AllFunctionActivity extends ZZTongActivity implements ApiNoticeListener {
    private ArrayList<String> mBasicData, mOtherData = null;
    private GridView basic_function_gw;//基本应用
    private GridView other_function_gw;//增值应用
    private IconBaseAdapter mIconBaseAdapter, mOtherAdapter;

    //    private DataBaseComponentModel mDBModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_all_function);
        super.onCreate(savedInstanceState);

        mBasicData = new ArrayList<String>();
        mOtherData = new ArrayList<String>();
        doShowBasicComponentItem();
        doShowOtherComponentItem();
//        mBasicData.add(FunctionInfo.FUNCTIONID[0]);
//        Resources res = getResources();
//        boolean isShowScannerCodePay = res.getBoolean(R.bool.isShowScannerCodePay);
//        if (isShowScannerCodePay) {
//            //扫码收款\
//            mBasicData.add(FunctionInfo.FUNCTIONID[22]);
//        }
//        List<ComponentMappingItemBean> list = null;
//         mDBModel = new DataBaseComponentModel(this);
//         list = mDBModel.queryComponentMappingByShowArea("2");
//        for(ComponentMappingItemBean bean:list){
//            mBasicData.add(bean.getItemCode());
//        }


//        list = mDBModel.queryComponentMappingByShowArea("3");
//        for(ComponentMappingItemBean bean:list){
//            mOtherData.add(bean.getItemCode());
//
//        }


//        //代还信用卡
//        mOtherData.add(FunctionInfo.FUNCTIONID[23]);
//        //银联转账
//        //data.add(FunctionInfo.FUNCTIONID[1]);
//        //信用卡还款
//        //data.add(FunctionInfo.FUNCTIONID[2]);
//        //贷款
//        mOtherData.add(FunctionInfo.FUNCTIONID[24]);
//        //分销系统
//        mOtherData.add(FunctionInfo.FUNCTIONID[19]);
//        //捷安借贷
//        mOtherData.add(FunctionInfo.FUNCTIONID[25]);
//        //证券开户
//        mOtherData.add(FunctionInfo.FUNCTIONID[21]);
//        //话费充值
//        mOtherData.add(FunctionInfo.FUNCTIONID[4]);
//        boolean isShowQQConsultation = res.getBoolean(R.bool.isShowQQConsultation);
//        if (isShowQQConsultation) {
//            mOtherData.add(FunctionInfo.FUNCTIONID[16]);
//        }

        initView();
        ApiNoticeFactory.getInstance().registerApiNoticeListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ApiNoticeFactory.getInstance().unregisterApiNoticeListener(this);
    }

    private void initView() {
        basic_function_gw = (GridView) findViewById(R.id.basic_function_gw);
        mIconBaseAdapter = new IconBaseAdapter(this);
        basic_function_gw.setAdapter(mIconBaseAdapter);
        mIconBaseAdapter.setData(mBasicData);


        other_function_gw = (GridView) findViewById(R.id.other_function_gw);
        mOtherAdapter = new IconBaseAdapter(this);
        other_function_gw.setAdapter(mOtherAdapter);
        mOtherAdapter.setData(mOtherData);

        basic_function_gw.setOnItemClickListener(new IconBaseOnItemClickListener(this));
        other_function_gw.setOnItemClickListener(new IconBaseOnItemClickListener(this));

    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(getString(R.string.all_function_title), true);
    }

    @Override
    public void onNotifyNotice(int action, Bundle bundle) {
        HPLog.i(HPLog.YL_TAG, "HomeFragment onNotifyNotice Action:" + action + " Bundle:" + bundle);
        if (action == REFRESH_COMPONENT_ACTION) {
            doShowBasicComponentItem();
            doShowOtherComponentItem();
            mIconBaseAdapter.setData(mBasicData);
            mOtherAdapter.setData(mOtherData);
        }
    }

    private void doShowBasicComponentItem() {
        try {
            List<ComponentMappingItemBean> list = null;
            DataBaseComponentModel mDBModel = null;
            mDBModel = new DataBaseComponentModel(this);
            list = mDBModel.queryComponentMappingByShowArea("2");
            mBasicData.clear();
            if (list.size() != 0) {
                for (ComponentMappingItemBean bean : list) {
                    mBasicData.add(bean.getItemCode());
                }
            } else {
                new RuntimeException("Component Item Size == 0");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mBasicData.add(FunctionInfo.COLLECT_MONEY_CODE);
            mBasicData.add(FunctionInfo.SCAN_PAYMENT_CODE);
            mBasicData.add(FunctionInfo.LOAN_PACKAGE_CODE);
        }
    }

    private void doShowOtherComponentItem() {
        try {
            List<ComponentMappingItemBean> list = null;
            DataBaseComponentModel mDBModel = null;
            mDBModel = new DataBaseComponentModel(this);
            list = mDBModel.queryComponentMappingByShowArea("3");
            mOtherData.clear();
            if (list.size() != 0) {

                for (ComponentMappingItemBean bean : list) {
                    mOtherData.add(bean.getItemCode());
                }
            } else {
                new RuntimeException("Component Item Size == 0");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mOtherData.add(FunctionInfo.MONG_HOO_CODE);
            mOtherData.add(FunctionInfo.LOAN_CODE);
            mOtherData.add(FunctionInfo.HONG_SHOP_CODE);
            mOtherData.add(FunctionInfo.JIEAN_CODE);
            mOtherData.add(FunctionInfo.SCAN_PAYMENT_CODE);
            mOtherData.add(FunctionInfo.MOBILE_RECHARGE_CODE);
            mOtherData.add(FunctionInfo.QQ_IM_CODE);
        }
    }

    @Override
    public int[] getNoticeAction() {
        int actions[] = {REFRESH_COMPONENT_ACTION};
        return actions;
    }
}
